import { Request, errorToast, successToast, checkPermission } from '@/utils/'
import { connectStatusList, allotStatusList } from '@/config'
import pagination from '@/components/pagination'
import signal from '@/components/signal'
let request = new Request()
export default {
	name : 'device',
	data() {
		return {
			request : null,
			connectStatusList : connectStatusList,
			allotStatusList : allotStatusList,
			gatewayList : [],
			filter : {
				online : '',
				gateway : '',
				allot : '',
				searchType : 1,
				device : ''
			},
			
			pageQuery : {
				
			},
			
			deviceIds : [],
			deviceList : [],
			
			allotParams : {
				agentId : '',
				storeId : '',
				priceId : '',
				cardCoinPriceId : ''
			},
			
			showAllotAgentDiaLog : false, //是否显示分配代理的弹窗
			allotAgentList : [],
			
			showAllotStoreDiaLog : false, //是否显示分配门店的弹窗
			allotStoreList : [],
			
			
			showSetChargePriceDiaLog : false, //是否显示设置价格的弹窗
			chargePriceList : [],
			
			showConfirmUnbindDeviceDiaLog : false, //是否显示解绑设备确认对话框
			
		}
	},
	
	components : {
		pagination,
		signal
	},
	
	methods : {
		checkPermission : checkPermission,
		search : function() {
			let filter = Object.assign({}, this.filter)
			
			if(filter.searchType == 1){
				filter.deviceNo = filter.device
			}else if(filter.searchType == 2){
				filter.deviceName = filter.device
			}else if(filter.searchType == 3){
				filter.simIccId = filter.device
			}
			
			
			delete filter.searchType
			delete filter.device
			
			this.pageQuery = Object.assign({}, filter)
			
			this.$nextTick(() => {
				this.$refs.pagination.initPage()
			})
			
			
		},
		
		pageList : function(rows){
			rows.forEach(v => {
				if(v.charge_price){
					if(v.charge_price.options){
						if(!Array.isArray(v.charge_price.options)){
							v.charge_price.options = JSON.parse(v.charge_price.options)
						}
					}
					
					if(v.charge_price.class_rate){
						if(!Array.isArray(v.charge_price.class_rate)){
							v.charge_price.class_rate = JSON.parse(v.charge_price.class_rate)
						}
					}
				}
			})
			this.deviceList = rows
		},
		
		handleSelectionChange : function(values){ //全选
			this.deviceIds.splice(0)
			values.forEach(v => {
				this.deviceIds.push(v.device_id)
			})
		},
		
		openAllotAgent : function(){ //打开分配代理商的弹窗
			request.get('agent/dropSelect', res => {
				if(res.success){
					this.allotAgentList = res.data
					this.showAllotAgentDiaLog = true
				}else{
					errorToast(res.msg ,this)
				}
			})
		},
		
		allotAgent : function(){ //分配代理商
			let deviceIds = this.deviceIds
			if(deviceIds.length <= 0){
				errorToast('请选择设备', this)
				return
			}
			
			request.post('device/allotAgent', res => {
				if(res.success){
					this.showAllotAgentDiaLog = false
					successToast('分配成功', this)
					this.$refs.pagination.getPage()
				}else{
					errorToast(res.msg ,this)
				}
			}, {agentId : this.allotParams.agentId, deviceIds : deviceIds})
			
		},
		
		openAllotStore : function(){ //打开分配门店的弹窗
			request.get('store/dropSelect', res => {
				if(res.success){
					this.allotStoreList = res.data
					this.showAllotStoreDiaLog = true
				}else{
					errorToast(res.msg ,this)
				}
			})
		},
		
		allotStore : function(){ //分配门店
			let deviceIds = this.deviceIds
			if(deviceIds.length <= 0){
				errorToast('请选择设备', this)
				return
			}
			
			request.post('device/allotStore', res => {
				if(res.success){
					this.showAllotStoreDiaLog = false
					successToast('分配成功', this)
					this.$refs.pagination.getPage()
				}else{
					errorToast(res.msg ,this)
				}
			}, {storeId : this.allotParams.storeId, deviceIds : deviceIds})
			
		},
		
		openSetChargePrice : function(){ //打开设置价格的弹窗
			request.get('price/dropSelect', res => {
				if(res.success){
					this.chargePriceList = res.data
					this.showSetChargePriceDiaLog = true
				}else{
					errorToast(res.msg, this)
				}
			})
		},
		
		setChargePrice : function(){ //设置价格
			let deviceIds = this.deviceIds
			if(deviceIds.length <= 0){
				errorToast('请选择设备', this)
				return
			}
			request.post('device/setPrice', res => {
				if(res.success){
					successToast('设置成功', this)
					this.showSetChargePriceDiaLog = false
					this.$refs.pagination.initPage()
				}else{
					errorToast(res.msg, this)
				}
			}, {priceId : this.allotParams.priceId, deviceIds : deviceIds})
		},
		
		sendDeviceParams : function(){
			let deviceIds = this.deviceIds
			if(deviceIds.length <= 0){
				errorToast('请选择设备', this)
				return
			}
			request.post('device/sendChargeRule', res => {
				if(res.success){
					successToast('下发成功', this)
				}else{
					errorToast(res.msg, this)
				}
			}, {deviceIds : deviceIds})
		},
		
		openUnbindDevice : function(){
			this.showConfirmUnbindDeviceDiaLog = true
		},
		
		unbindDevice : function(){
			let deviceNos = this.deviceNos
			
			if(deviceNos.length == 0){
				errorToast('请选择设备' ,this)
				return
			}
			
			this.request.post('deviceManager/releaseDevice', res => {
				if(res.code == 0){
					this.showConfirmUnbindDeviceDiaLog = false
					successToast('解绑成功', this)
					this.$refs.pagination.getPage()
				}else{
					errorToast(res.msg ,this)
				}
			}, {deviceNos : deviceNos})
		},
		
		deleteDevice : function(deviceId){
			if(!confirm('确定删除该设备吗')){
				return
			}

			request.post('device/delete', res => {
				if(res.success){
					successToast('删除成功', this)
					this.$refs.pagination.getPage()
				}else{
					errorToast(res.msg ,this)
				}
			}, {deviceId : deviceId})
		},
		
		toggleDeviceLock (checked, scope) {
			let status = checked ? 1 : 0
			scope.lock_status = status
			request.post('device/updateDeviceLock', res => {
				if(res.success) {
					successToast('操作成功', this)
				}else{
					scope.lock_status = status == 1 ? 0 : 1
					errorToast(res.msg ,this)
				}
			}, {status : status, deviceId : scope.device_id})
		}
		
	},
	
	created() {
		/* this.querys.vehicleType = this.vehicleType
		this.queryParams.vehicleType = this.vehicleType
		this.request = new Request()
		this.request.get('deviceManager/getGatewayList', res => { //网关列表
			if(res.code == 0){
				this.gatewayList = res.data
			}
		}) */
		
	},
	
	activated () {
		if(!this.ISFIRST){
			this.ISFIRST = true
		}else{
			this.$refs.pagination.setInit(1)
			this.$refs.pagination.getPage()
		}
	}
	
}